package seek

import (
	"math"

	ab "git.cloud.inspur.com/ichain/ichain-protos-go/consenser"
)

type Type string

const (
	Oldest    = "oldest"
	Newest    = "newest"
	FromBlock = "from"
)

var (
	oldestPos = &ab.SeekPosition{Type: &ab.SeekPosition_Oldest{Oldest: &ab.SeekOldest{}}}
	newestPos = &ab.SeekPosition{Type: &ab.SeekPosition_Newest{Newest: &ab.SeekNewest{}}}
	maxPos    = &ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: math.MaxUint64}}}
)

func InfoOldest() *ab.SeekInfo {
	return newSeekInfo(oldestPos, maxPos)
}

func InfoNewest() *ab.SeekInfo {
	return newSeekInfo(newestPos, maxPos)
}

func InfoFrom(fromBlock uint64) *ab.SeekInfo {
	return newSeekInfo(seekFromPos(fromBlock), maxPos)
}

func seekFromPos(fromBlock uint64) *ab.SeekPosition {
	return &ab.SeekPosition{
		Type: &ab.SeekPosition_Specified{
			Specified: &ab.SeekSpecified{
				Number: fromBlock,
			},
		},
	}
}

func newSeekInfo(start *ab.SeekPosition, stop *ab.SeekPosition) *ab.SeekInfo {
	return &ab.SeekInfo{
		Start:    start,
		Stop:     stop,
		Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
	}
}
